package com.gxa.ssm340.service.impl;

import com.gxa.ssm340.common.ErrorCode;
import com.gxa.ssm340.entity.dto.CouponPageDTO;
import com.gxa.ssm340.entity.dto.CpnUsedPageDTO;
import com.gxa.ssm340.entity.dto.PageDTO;
import com.gxa.ssm340.entity.pojo.TbGet;
import com.gxa.ssm340.entity.vo.CpnUsedListVO;
import com.gxa.ssm340.entity.vo.UserCouponVO;
import com.gxa.ssm340.exception.BizException;
import com.gxa.ssm340.mapper.CpnCancelMapper;
import com.gxa.ssm340.mapper.UserCancelMapper;
import com.gxa.ssm340.service.UserCancelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Throwable.class)
public class UserCancelServiceImpl implements UserCancelService {
    @Autowired
    UserCancelMapper userCancelMapper;
    @Autowired
    CpnCancelMapper cpnCancelMapper;
    // @Override
    // public UserCouponVO[] selectAllLikePhone(CouponPageDTO couponPageDTO) throws BizException {
    //     UserCouponVO[] userCouponVO = userCancelMapper.selectAllLikePhone(couponPageDTO);
    //     return userCouponVO;
    // }
    @Override
    public UserCouponVO[] selectAllLikePhone(CouponPageDTO couponPageDTO) throws BizException {
        UserCouponVO[] userCouponVO = userCancelMapper.selectAllLikePhone(couponPageDTO);
        return userCouponVO;
    }

    // @Override
    // public UserCouponVO[] selectBypage(PageDTO pageDTO) throws BizException {
    //
    //     UserCouponVO[] userCouponVOS = userCancelMapper.selectBypage(pageDTO);
    //     return userCouponVOS;
    // }

    @Override
    public void modifyById(Integer id,CouponPageDTO couponPageDTO) {
        TbGet tbGet = new TbGet();
        for (UserCouponVO userCouponVO : cpnCancelMapper.selectAllByPhone(couponPageDTO.getPhone())) {
            tbGet.setCid(userCouponVO.getId());
            tbGet.setUid(userCouponVO.getUserId());
            tbGet.setPhone(userCouponVO.getPhone());
            tbGet.setGetTime(userCouponVO.getCollectExp());
            tbGet.setGetIsUsed(userCouponVO.getIsUse());
            tbGet.setGetUseTime(new Date());
        }
        // userCancelMapper.insertById(tbGet);
        // userCancelMapper.deleteById(id);
    }

    @Override
    public List<CpnUsedListVO> selectByTime(CpnUsedPageDTO cpnUsedPageDTO) throws BizException {

        List<CpnUsedListVO> cpnUsedListVOS = userCancelMapper.selectByTime(cpnUsedPageDTO);
        if(cpnUsedListVOS==null) {
            throw  new BizException(ErrorCode.NULL_USER);
        }
        return cpnUsedListVOS;
    }

    @Override
    public void updateById(Integer id) {
        Integer integer = userCancelMapper.updateById(id);
    }

    // @Override
    // public CpnUsedListVO[] selectAllTbGet() {
    //     return  userCancelMapper.selectAllTbGet();
    // }
    //

}
